package com.mySql;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;


@WebServlet("/loginServlet")
public class loginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/user";
            String username_1 = "root";
            String password_1 = "root";
            Connection conn = DriverManager.getConnection(url, username_1, password_1);
            String sql = "select * from user.userdate where username = '"+username+"'";
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(sql);
            while (rs.next()) {
                if (rs.getString("password").equals(password)) {
                    resp.sendRedirect("/showServlet");
                } else
                    resp.sendRedirect("Test/mysql/login/loginFailed.jsp");
            }
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}